//
//  HWContentImageView.m
//  JWC
//
//  Created by lanou on 16/4/11.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWContentImageView.h"
#import "UIImageView+WebCache.h"
#import "DALabeledCircularProgressView.h"
#import "HWRecommendModel.h"
#import "HWDetailPictureController.h"

@interface HWContentImageView ()

@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIButton *bigImgBtn;
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIImageView *baisiView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation HWContentImageView

+ (instancetype)imageView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

// 但加载.xib的时候走这方法
- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    // 给图片添加监听器
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPicture)]];
}

- (void)showPicture
{
    HWDetailPictureController *showPicVc = [[HWDetailPictureController alloc]init];
    showPicVc.model = self.model;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicVc animated:YES completion:nil];
}

// 重写set方法
- (void)setModel:(HWRecommendModel *)model
{
    _model = model;
    if ([model.type isEqualToString:@"image"])
    {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image.download_url.firstObject] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            self.bigImgBtn.hidden = YES;
            self.baisiView.hidden = NO;
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            self.progressView.hidden = NO;
            self.progressView.progressLabel.textColor = [UIColor whiteColor];
            self.progressView.roundedCorners = 3;
            self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%",progress*100];
            [self.progressView setProgress:progress animated:YES];
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.progressView.hidden = YES;
            self.baisiView.hidden = YES;
            
            // 判断是不是大图
            if (model.image.height / model.image.width > 2)
            {
                // 如果是的话 则截出图片的最上方铺满contentView
                // 开启图形上下文
                UIGraphicsBeginImageContextWithOptions(self.imageView.size, YES, 0.0);
                // 将下载完的image对象绘制到图形上下文
                CGFloat width = self.imageView.width;
                CGFloat height = width * image.size.height / image.size.width;
                [image drawInRect:CGRectMake(0, 0, width, height)];
                // 获得图片
                self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                // 结束图形上下文
                UIGraphicsEndImageContext();
                self.bigImgBtn.hidden = NO;
            }
            else
            {
                self.imageView.contentMode = UIViewContentModeScaleToFill;
                self.bigImgBtn.hidden = YES;
            }
        }];
    }
    else if ([model.type isEqualToString:@"gif"])
    {
        [self.progressView setProgress:0.0 animated:NO];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.gif.images.firstObject] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            self.bigImgBtn.hidden = YES;
            self.baisiView.hidden = NO;
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            self.progressView.hidden = NO;
            self.progressView.progressLabel.textColor = [UIColor whiteColor];
            self.progressView.roundedCorners = 3;
            self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%",progress*100];
            [self.progressView setProgress:progress animated:YES];
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.progressView.hidden = YES;
            self.imageView.contentMode = UIViewContentModeScaleToFill;
            self.bigImgBtn.hidden = YES;
        }];
    }
    
    self.gifView.hidden = ![model.type isEqualToString:@"gif"];
}
- (IBAction)bigButton:(UIButton *)sender
{
    [self showPicture];
}

@end






















